#include "client.h"

char                    *my_strcat(char *s1,
                                   char *s2)
{
  char                  *new = NULL;
  unsigned int          i = 0;
  unsigned int          j = 0;

  new = malloc(strlen(s1) + strlen(s2) * sizeof (char));
  while (i < strlen(s1))
    {
      new[i] = s1[i];
      i++;
    }
  while (j < strlen(s2))
    {
      new[i] = s2[j];
      i++;
      j++;
    }
  new[i] = '\0';
  return (new);
}

int extract_header(char *buffer)
{
  int ret = 0;

  if (buffer[0] == '1')
    ret = 1;
  else if (buffer[0] == '2')
    ret = 2;
  else if (buffer[0] == '3')
    ret = 3;

  strncpy(buffer, buffer + 2, 1022);

  return ret;
}

char *extract_fheader(char *buffer)
{
  char *filename = NULL;
  int pos = 0;
  int pos2 = 0;
  int i = 0;

  while (buffer[pos] != '\'')
    {
      pos++;
    }
  filename = malloc(pos);
  strncpy(filename, buffer, pos);
  while(filename[i] != '\0')
    {
      if (filename[i] == '/')
	pos2 = i;
      i++;
    }
  strncpy(filename, filename + pos2 + 1, strlen(filename) - pos2);
  filename[pos2 + 1] = '\0';
  strncpy(buffer, buffer + pos, strlen(buffer) - pos);

  for (i=strlen(buffer) - pos; i < strlen(buffer); i++)
      buffer[i] = '\0';

  return filename;
}

void	write_file(char *buffer, int sock, struct sockaddr *ssock)
{
  char *file  = NULL;
  FILE *f = NULL;
  int len = sizeof(ssock);

  extract_header(buffer);
  file = extract_fheader(buffer);
  file = my_strcat(CURRENT_DIR, file);
  f = fopen(file, "w");

  if (f == NULL)
    {
      printf("Problem création du fichier\n");
      perror("fopen");
      return;
    }
  while(strcmp(buffer, "-1\0") !=0)
    {
      fwrite(buffer + 1, 1, strlen(buffer), f);
      memset(buffer, '\0', sizeof(buffer));
      if (recvfrom(sock, buffer, sizeof(buffer), 0, (struct sockaddr *) &ssock, &len) < 0)
	{
	  printf("erreur reading file\n");
	  return;
	}
    }
  fflush(f);
  fclose(f);
}
	

void client_unicast(char *DestIp, int DestPort)
{
  struct sockaddr_in fsock = {0};
  int	sockfd;
  char buffer[1024] = {0};
  char *cmp = malloc(2 * sizeof(char));

  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd == -1)
    {
      printf("Echec creation socket()");
      return;
    }

  //  fsock = malloc(sizeof(struct sockaddr_in));
  fsock.sin_addr.s_addr = inet_addr(DestIp);
  fsock.sin_family = AF_INET;
  fsock.sin_port = htons(DestPort);


  if(connect(sockfd, (struct sockaddr *) &fsock, sizeof(fsock)) != -1)
    {
      printf("Connecté\n");
      int len = sizeof(fsock);

      while(1)
	{
	  memset(buffer, '\0', 1024);
	  int i = 0;
	  if (recvfrom(sockfd, buffer, 1024, 0, (struct sockaddr *) &fsock, &len) != -1)
	    {
	      cmp = strncpy(cmp, buffer, 2);
	      if (strcmp(cmp, "10") == 0)
		write_file(buffer, sockfd, (struct sockaddr *) &fsock);
	    }
	  else
	    {
	      perror("recv");
	      printf("error");
	    }
	}
    }
  else
    {
      perror("accept");
      printf("Connection impossible\n");
    }

  close(sockfd);
}


int main()
{
  client_unicast("10.41.170.65", 33333);
  return 0;
}
